<template>
  <button @click="btnClick">点击</button>
  <div class="text" v-echart="opt">
    <!-- <Echarts :options="opt" /> -->
  </div>
</template>

<script>
import * as echarts from "echarts";
import vue from "vue";
import Echarts from "./useEcharts.vue";
export default {
  components: {
    // Echarts,
  },
  directives: {
    echart: {
      mounted(el, bindings, vnode) {
        const { value, instance } = bindings;
        const einstance = echarts.init(el);
        console.log("自定义指令", vnode);
        instance.$watch(
          () => value,
          () => {
            einstance.setOption(value);
          },
          {
            deep: true,
            immediate: true,
          }
        );
      },
    },
  },
  data() {
    return {
      opt: {
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      },
    };
  },
  methods: {
    btnClick() {
      this.opt.title.text += "888";
    },
  },
  watch: {
    opt: {
      handler(newValue) {
        console.log("监听数据", newValue);
      },
      deep: true,
    },
  },
};
</script>

<style scoped>
.text {
  width: 400px;
  height: 400px;
  background-color: #ccc;
}
</style>
